package com.example.demo06resource.component;

import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

/***
 * @Author 徐庶   QQ:1092002729
 * @Slogan 致敬大师，致敬未来的你
 *
 * 延迟特性  和  分组特性
 */
public class MyDeferredImportSelector implements DeferredImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.example.demo06resource.component.UserComponent"};
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        return null;
    }

    @Override
    public Class<? extends Group> getImportGroup() {
        // 这个返回值决定调用 DeferredImportSelector.selectImports，还是调用Group.selectImports
        // 如果返回 null，调用 DeferredImportSelector.selectImports
        return MyGroup.class;
    }

    /**
     * 如果 getImportGroup 返回自定义的 Group，会调用自定的义 Group 的 process 方法
     * 如果 getImportGroup 返回 null，会调用 DefaultDeferredImportSelectorGroup 的 process 方法，即调用 selectImports
     * 分组利用归类，同一组的bean只影响本组的顺序
     */
    private static class MyGroup implements Group {

        AnnotationMetadata metadata;

        @Override
        public void process(AnnotationMetadata metadata, DeferredImportSelector selector) {
            this.metadata = metadata;
        }

        @Override
        public Iterable<Entry> selectImports() {
            List<Entry> list = new ArrayList<>();
            list.add(new Entry(this.metadata, "com.example.demo06resource.component.UserComponent"));
            return list;
        }
    }
}
